package org.bigjava.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.bigjava.bean.User;
import org.bigjava.bean.Xiaoqu;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository("userMapper")
public interface UserMapper {

    //业主账号验证是否存在
    @Select("select * from user where jsc=1 and username=#{username}")
    public User userJiaoYan(String username);

    //根据账号和业主姓名查询业主
    @Select("select * from user where jsc=1 and username like '%' #{username} '%' and realname like '%' #{realname} '%'")
    @Results({
            @Result(property ="xiaoqu",column = "x_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public List<User> userMoHu(String username,String realname);

    //业主根据id个人信息修改
    @Update("update user set username=#{username},password=#{password},phone=#{phone},realname=#{realname},jtwz=#{jtwz},x_id=#{x_id} where id=#{id}")
    public int userXiuGai(User user);

    //根据id查出业主信息
    @Select("select * from user where id = #{id}")
    @Results({
            @Result(property ="xiaoqu",column = "x_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public User userId(int id);

    //根据id假删除业主
    @Update("update user set jsc=0 where id=#{id}")
    public int userShanChu(int id);

/*
    //根据id业主删除
    @Delete("delete from user where id = #{id}")
    public void usershanchu(int id);
*/

    //业主总人数
    @Select("select count(*) from user where jsc=1")
    public int userZong();

    //业主分页查询
    @Select("select * from user where jsc=1 limit #{offset},#{length}")
    @Results({
            @Result(property ="xiaoqu",column = "x_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public List<User> userFenYe(int offset , int length);

    //业主账号密码登录
    @Select("select * from user where username=#{username} and password=#{password} and jsc=1")
    @Results({
            @Result(property ="xiaoqu",column = "x_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public User userDengLu(String username, String password);

    //查询全部业主
    @Select("select * from user")
    @Results({
            @Result(property ="xiaoqu",column = "x_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public List<User> userQuan();

    //添加业主
    @Insert("insert into user(username,password,phone,realname,registeDate,jtwz,x_id)" +
            " values (#{username},#{password},#{phone},#{realname},#{registeDate},#{jtwz},#{x_id})")
    public int userAdd(User user);
}
